//
//  ChufangTableViewCell.m
//  TY-C
//
//  Created by xyyl on 2024/4/9.
//

#import "ChufangTableViewCell.h"
#import "ComonFile.h"

@implementation ChufangTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initViews];

        self.contentView.backgroundColor = RGBColor(250, 250, 250);
    }
    return self;
}

- (void)setCellWidth:(CGFloat)cellWidth {
    _cellWidth = cellWidth;
    [self initFrames];
}

- (void)initFrames {
    _whiteLab.frame = CGRectMake(0, 0, self.cellWidth, 4 * IPADSCALE);
    _whiteLab.backgroundColor = [UIColor whiteColor];
    [_selBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(10*IPADSCALE));
        make.centerY.equalTo(self.cfmcLab.mas_centerY);
        make.left.equalTo(self.contentView).offset(7.5 * IPADSCALE);
    }];
    [_selBtn setBackgroundImage:[UIImage imageNamed:@"icon_checkbox"] forState:0];

    [_selBtn setBackgroundImage:[UIImage imageNamed:@"icon_checkbox_checked"] forState:UIControlStateSelected];
    [_selBtn addTarget:self action:@selector(selfBtnAction) forControlEvents:UIControlEventTouchUpInside];

    CGFloat www = _cellWidth;
    CGFloat hhh = 27*IPADSCALE;
    NSArray * aaa =
    @[_cfmcLab,_cflxLab,_plLab,_mkLab,_zlsjLab,_cxLab,_jgLab,_ssLab,_xjLab,_ycLab];
    NSArray * bbb = @[_fscsBtn,_bjBtn,_scBtn];
    NSArray * imgCC = @[@"cf_fscs",@"cf_bj",@"cf_sc"];///noricon
    NSArray * imgDD = @[@"cf_fscsSel",@"cf_bjSel",@"cf_scSel"];///sel icon

    CGFloat space = 5 *IPADSCALE;
    CGFloat viewWWW = (www - 20 * IPADSCALE - space *13)/30;
    CGFloat oriX = 20 * IPADSCALE;
    for (int i = 0; i <aaa.count ; i++) {
        UILabel * ll = aaa[i];
        if(i == 0||i == 1||i == 4||i == 10) {
            ll.frame = CGRectMake(oriX, CGRectGetMaxY(_whiteLab.frame), viewWWW * 3, hhh);

        }else{
            ll.frame = CGRectMake(oriX, CGRectGetMaxY(_whiteLab.frame), viewWWW * 2, hhh);

        }
//        ll.text = @"2222";
        ll.textAlignment = NSTextAlignmentCenter;
        ll.numberOfLines = 0;
        ll.font = ZYQFONT(18);
        ll.textColor = RGBColor(102, 102, 102);
        
        oriX = CGRectGetMaxX(ll.frame) + space;
    }
    for (int i = 0; i<bbb.count; i++) {
        UIButton * btn = bbb[i];
        if(i == 0) {
            btn.frame = CGRectMake(oriX, CGRectGetMaxY(_whiteLab.frame), viewWWW * 3, hhh);

        }else{
            btn.frame = CGRectMake(oriX, CGRectGetMaxY(_whiteLab.frame), viewWWW * 2, hhh);

        }
        [btn setImage:[UIImage imageNamed:imgCC[i]] forState:0];
        [btn setImage:[UIImage imageNamed:imgDD[i]] forState:UIControlStateSelected];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        oriX = CGRectGetMaxX(btn.frame) + space;
    }
    
}

- (void)setPModel:(People *)pModel {
    _pModel = pModel;
    self.cfmcLab.text = [NSString stringWithFormat:@"%@",pModel.cfmc];
    self.cflxLab.text = pModel.cflx;
    self.plLab.text = [NSString stringWithFormat:@"%@Hz",pModel.pl];
    self.mkLab.text = [NSString stringWithFormat:@"%@μs",pModel.mk];
    self.zlsjLab.text = [NSString stringWithFormat:@"%@min",pModel.zlsj];
    self.cxLab.text = [NSString stringWithFormat:@"%@s",pModel.cxsj];
    self.jgLab.text = [NSString stringWithFormat:@"%@s",pModel.jgsj];
    self.ssLab.text = [NSString stringWithFormat:@"%@s",pModel.sssj];
    self.xjLab.text = [NSString stringWithFormat:@"%@s",pModel.xjsj];
    self.ycLab.text = [NSString stringWithFormat:@"%@s",pModel.ycsj];
    
    _fscsBtn.selected = [pModel.isfscs integerValue];
    _scBtn.selected  = [pModel.isdel integerValue];
    _bjBtn.selected = [pModel.isbj integerValue];
    _selBtn.selected = pModel.isSel;
    
}

- (void)btnAction:(UIButton *)btn {
    NSInteger ttt = btn.tag;
    if(btn.selected == NO){
        return;
    }
    if(ttt == 0){
        ///发送参数
    }
    if(ttt == 1){
        ///编辑
        
    }
    if(ttt == 2){
        ///删除
        [TY_DataCommand delDataWithCreateID:_pModel.creatTime];
        [[NSNotificationCenter defaultCenter] postNotificationName:ChuFangRefresh object:nil];
    }
}

- (void)selfBtnAction {
    if ([_pModel.isdel isEqualToString:@"0"]) {
        return;
    }
    
    _selBtn.selected = !_selBtn.selected;
    _pModel.isSel = _selBtn.selected;
}

- (void)initViews {
    _whiteLab = [UILabel new];
    _selBtn = [UIButton new];
    _cfmcLab = [UILabel new];
    _cflxLab = [UILabel new];
    _plLab = [UILabel new];
    _mkLab = [UILabel new];
    _zlsjLab = [UILabel new];
    _cxLab = [UILabel new];
    _jgLab = [UILabel new];
    _ssLab = [UILabel new];
    _xjLab = [UILabel new];
    _ycLab = [UILabel new];
    _fscsBtn = [UIButton new];
    _bjBtn = [UIButton new];
    _scBtn = [UIButton new];

    
    [self.contentView addSubview:_whiteLab];
    [self.contentView addSubview:_selBtn];
    [self.contentView addSubview:_cfmcLab];
    [self.contentView addSubview:_cflxLab];
    [self.contentView addSubview:_plLab];
    [self.contentView addSubview:_mkLab];
    [self.contentView addSubview:_zlsjLab];
    [self.contentView addSubview:_cxLab];
    [self.contentView addSubview:_jgLab];
    [self.contentView addSubview:_ssLab];
    [self.contentView addSubview:_xjLab];
    [self.contentView addSubview:_ycLab];
    [self.contentView addSubview:_fscsBtn];
    [self.contentView addSubview:_bjBtn];
    [self.contentView addSubview:_scBtn];

    
    
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}


@end
